package com.anti.commom.core.utils;


import com.anti.commom.core.utils.supports.ParamMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2020-10-24
 */
public class RequestUtils {
    public RequestUtils() {
    }

    public static int getParameter(HttpServletRequest request, String name, int defaultValue) {
        if (request == null) {
            return defaultValue;
        } else {
            String value = request.getParameter(name);
            return StringUtils.isBlank(value) ? defaultValue : Integer.parseInt(value);
        }
    }

    public static long getParameter(HttpServletRequest request, String name, long defaultValue) {
        if (request == null) {
            return defaultValue;
        } else {
            String value = request.getParameter(name);
            return StringUtils.isBlank(value) ? defaultValue : Long.parseLong(value);
        }
    }

    public static String getParameter(HttpServletRequest request, String name, String defaultValue) {
        if (request == null) {
            return defaultValue;
        } else {
            String value = request.getParameter(name);
            return StringUtils.isBlank(value) ? defaultValue : value;
        }
    }

    public static void addFrameSupport(HttpServletResponse response) {
        response.setHeader("P3P", "CP=CAO PSA OUR");
    }

    public static String getRemoteIP(HttpServletRequest request) {
        String ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }

        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }

        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
                try {
                    InetAddress inet = InetAddress.getLocalHost();
                    ipAddress = inet.getHostAddress();
                } catch (UnknownHostException var4) {
                    var4.printStackTrace();
                }
            }
        }

        if (ipAddress != null && ipAddress.length() > 15 && ipAddress.indexOf(",") > 0) {
            ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
        }

        return ipAddress;
    }

    public static ParamMap getParamMap(HttpServletRequest request) {
        ParamMap paramMap = new ParamMap();
        Enumeration enu = request.getParameterNames();

        while(enu.hasMoreElements()) {
            String key = (String)enu.nextElement();
            if (!"_".equals(key)) {
                String value = request.getParameter(key);
                if (value != null) {
                    value = value.trim();
                }

                if (StringUtils.isNotBlank(value)) {
                    paramMap.put(key, value);
                }
            }
        }

        return paramMap;
    }
}
